let server = require('express')();
let mysql = require('mysql');
let estatic = require('express-static');
const port = 8080;
let sql = mysql.createConnection({
	host: 'localhost',
	user: 'root',
	password: '123456',
	database: 'peak',
	timezone: '08:00',
	port: 3306
})

sql.connect();
// 注册
server.get("/reg", function(request, response) {
	let username = request.query.username;
	let password = request.query.password;
	sql.query(`SELECT * FROM user WHERE username="${username}"`, function(error, data) {
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		if (data.length === 0) {
			sql.query(`INSERT INTO user (username,password) VALUES ("${username}","${password}")`, function(error, result) {
				if (error) {
					console.log(error);
					response.end("error");
					return;
				}
				response.end("success");
			})
		} else {
			response.end("exist")
		}
	})
})

// 登录
server.get("/login", function(request, response) {
	let username = request.query.username;
	let password = request.query.password;
	sql.query(`SELECT * FROM user WHERE username="${username}"`, function(error, data) {
		if (data.length === 0) {
			response.end("error");
			return;
		}
		if (data[0].password === password) {
			response.end(JSON.stringify(data[0].id))
		} else {
			response.end("error")
		}
	})
})

// 首页显示商品数据
server.get("/goods", function(request, response) {
	sql.query(`SELECT * FROM goods`, function(error, data) {
		if (data.length === 0) {
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data))
	})
})

//详情页数据
server.get("/goodsItem", function(request, response) {
	let id = request.query.id;
	sql.query(`SELECT * FROM goods WHERE id=${id}`, function(error, data) {
		if (data.length === 0) {
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data))
	})
})

// 购物车插入用户购物数据
server.get("/insert", function(request, response) {
	let id = request.query.id;
	let goodsID = request.query.goodsID;
	let userID = request.query.userID;
	let number = request.query.number;
	let key = request.query.key;
	let value = request.query.value;
	sql.query(`SELECT * FROM shops WHERE goodsID="${goodsID}"`, function(error, data) {
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		if (data.length === 0) {
			sql.query(`INSERT INTO shops (id,goodsID,userID,number) VALUES ("${id}","${goodsID}", "${userID})", "${number}")`, function(error, result) {
				if (error) {
					console.log(error);
					response.end("error");
					return;
				}
				response.end("success");
			})
		} else {
			sql.query(`UPDATE peak.shops SET ${key}="${value}" WHERE id = ${id}`, function(error, data) {
				if (data.length === 0) {
					response.end("error");
					return;
				}
				response.end("success!!");
			})
		}
	})
})



// 购物车数据渲染
server.get('/shopdata',function(request,response){
	let id = request.query.id;
	sql.query(`SELECT user.id,userID,goodsID,username,pic,title,price,stock,number FROM goods,shops,user WHERE user.id=${id} AND user.id=userID AND goods.id=goodsID`,function(error,data){
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data))
	})
})

//购物车删除数据
server.get("/deleteItem", function(request, response) {
	let id = request.query.id;
	sql.query(`DELETE FROM peak.shops WHERE id = ${id}`, function(error, data) {
		if (data.length === 0) {
			response.end("error");
			return;
		}
		response.end("success!!");
	})
})

server.use(estatic(__dirname + "/static"))

server.listen(port);
console.log(`server is running at ${port}`)
